Refactor: Extract headless runner from create-expert CLI#347
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
d610e6e to
a8445dc
Compare
- Extract headless execution logic to src/lib/headless-runner.ts - Reduce CLI file size from 377 to 230 lines (39% reduction) - CLI now only handles argument parsing and validation - Enable unit testing of headless execution logic 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
a8445dc to
ce0f6b3
Compare
|
|
||
| resolve({ | ||
| success: !failed, | ||
| exitCode: failed ? 1 : 0, |
There was a problem hiding this comment.
Inconsistent exitCode returned between JSON and non-JSON modes
The exitCode field in HeadlessRunnerResult behaves inconsistently between code paths. When jsonOutput is true, the actual process exit code is returned. When jsonOutput is false, the value is normalized to 0 or 1 via failed ? 1 : 0. This means the same API returns different data depending on the output mode. Additionally, the log at line 145 displays the actual exit code while the returned result contains a different normalized value, creating a debugging mismatch.
Additional Locations (1)
| process.exit(failed ? 1 : 0) | ||
| }) // End of close handler | ||
| } | ||
| process.exit(result.success ? 0 : 1) |
There was a problem hiding this comment.
JSON mode no longer preserves actual process exit codes
The original JSON mode used process.exit(code || 0) which preserved actual non-zero exit codes from the child process (e.g., 137 for OOM kills). After the refactoring, the CLI does process.exit(result.success ? 0 : 1), which normalizes all failures to exit code 1. Users relying on specific exit codes to detect different failure modes (such as signal-killed processes or specific error conditions) will now always receive 1 for any failure.
Summary
src/lib/headless-runner.ts(159 lines)Closes #344
Test plan
pnpm typecheckpassespnpm testpassespnpm buildsucceedsnpx create-expert --headless --description "test"🤖 Generated with Claude Code
Note
Refactors headless mode into a reusable module and simplifies the CLI.
src/lib/headless-runner.tsencapsulates headless execution: spawnsperstack run, supports JSON vs human-readable output, parses events, aggregates result/errors, and returns a structured statusbin/cli.tsnow focuses on arg parsing/validation, project generation for new projects, query construction, and delegates execution torunHeadlessExecution; exits based on returned successrunHeadlessExecutionand its types viasrc/index.tsWritten by Cursor Bugbot for commit e10a126. This will update automatically on new commits. Configure here.